Built-in Modules > UPS Class
UPS Class
Important: This plugin relies on third-party resources. The resources might change or be discontinued without notice. Ensure you test the plugin extensively before production use, implement error handling with a try-catch block, have a backup shipping rate ready, and reach out to our support with detailed descriptions of any unexpected issues.
Overview
The UPS
class is tailored for fetching real-time shipping rates from UPS.
Class Definition
class UPS {
constructor(origin, destination, currency, order_processing_days, dutyValue) {
// Class constructor
}
async getRates(parcels) {
// Method to fetch rates
}
}
Arguments
origin
(Object): Represents the "origin" data in the DATA JSON object used by thecalculateShippingRates
function.destination
(Object): Represents the "destination" data in the same DATA JSON object.currency
(String): The currency ISO code for shipping rate calculations.order_processing_days
(Number): The number of days until parcels are ready for pickup.dutyValue
(Number): The customs duty value of the parcels in the currency used for shipping rate calculations. It is a whole number.
Usage Example
import { UPS } from "./modules.js"
const ups = new UPS(DATA.origin, DATA.destination, DATA.currency, 3, 120);
const parcels = [
{ length: 10, width: 10, height: 5, metricUnit: "CM", weight: 10, weightType: "KGS", declaredValue: 120, quantity: 2 },
// ... more parcels
];
const rates = await ups.getRates(parcels);
This example demonstrates how to initialize the UPS
class with origin, destination, currency, order processing days, and duty value. The getRates
method calculates shipping rates for an array of parcels, each defined by dimensions, weight, declared value (in the currency used for shipping rate calculations), and quantity.
Notes
- For parcels in origin countries using the metric system, the values for
metricUnit
andweightType
should be 'CM' and 'KGS', respectively. - For parcels in origin countries using the imperial system, the values should be 'IN' and 'LBS', respectively.
- The parcels dimension,
dutyValue
anddeclaredValue
must be given in whole number. The weight can have decimals. - The plug-in assumes the Shipment Type is not UPS Import Control or UPS Returns.
The structure of the returned rates
is consistent with Shopify's shipping rates format, ensuring easy integration into your e-commerce platform.
The rates are based on the information on UPS website.
Sample AI Prompt
I'd like a shipping rate calculator that uses the UPS rates.
The parcels will be ready for pick up in 3 days. The customs duty value is $200.
The dimensions, weight and declared values of the parcels are contained in the items metafield namespace object called "parcel". Get the first 15 values in the namespace.
The origin country uses a metric unit system.
Sample AI Output
import { enrichItemDetails, UPS } from './modules.js';
export async function calculateShippingRates(DATA, env) {
try {
// Enriching DATA with additional parcel information
DATA = await enrichItemDetails(DATA, [{ namespace: "parcel", size: 15 }]);
// Extracting parcels information from enriched DATA
const parcels = DATA.items.map(item => ({
length: item.metafields.parcel.length,
width: item.metafields.parcel.width,
height: item.metafields.parcel.height,
metricUnit: "CM",
weight: item.metafields.parcel.weight,
weightType: "KGS",
declaredValue: item.metafields.parcel.declaredValue,
quantity: item.quantity
}));
// Setting up UPS module with necessary arguments
const ups = new UPS(DATA.origin, DATA.destination, DATA.currency, 3, 200);
// Fetching rates from UPS
const rates = await ups.getRates(parcels);
// Returning rates in the expected Shopify format
return { rates };
} catch (error) {
// In case of any error, return a rate with price equals to null and other fields set to empty string
return {
rates: [{
service_name: "",
service_code: "",
total_price: null,
description: "",
currency: "",
min_delivery_date: "",
max_delivery_date: ""
}]
};
}
}